|
Download
FAQ History |
|
API
Search Feedback |
Accessing Databases from Web Applications
Data that is shared between Web components and persistent between invocations of a Web application is usually maintained in a database. Web applications use the JDBC 2.0 API to access relational databases. For information on this API, see
In the JDBC API, databases are accessed via
DataSourceobjects. ADataSourcehas a set of properties that identify and describe the real world data source that it represents. These properties include information like the location of the database server, the name of the database, the network protocol to use to communicate with the server, and so on.Applications access a data source using a connection, and a
DataSourceobject can be thought of as a factory for connections to the particular data source that theDataSourceinstance represents. In a basicDataSourceimplementation, a call to the methodDataSource.getConnectionreturns a connection object that is a physical connection to the data source.If a
DataSourceobject is registered with a JNDI naming service, an application can use the JNDI API to access thatDataSourceobject, which can then be used to connect to the data source it represents.
DataSourceobjects that implement connection pooling also produce a connection to the particular data source that theDataSourceclass represents. The connection object that the methodDataSource.getConnectionreturns is a handle to aPooledConnectionobject rather than being a physical connection. An application uses the connection object like any other connection. Connection pooling has no effect on application code except that a pooled connection, like all connections, should always be explicitly closed. When an application closes a connection that is pooled, the connection is returned to a pool of reusable connections. The next timeDataSource.getConnectionis called, a handle to one of these pooled connections will be returned if one is available. Because connection pooling avoids creating a new physical connection every time one is requested, it can help applications run significantly faster.The Duke's Bookstore examples use the PointBase evaluation database included with the J2EE 1.4 Application Server to maintain the catalog of books. This section describes how to:
Starting the PointBase Database Server
To start the PointBase database server:
On Windows, from the Start menu, choose
Programs
Sun Microsystems
J2EE 1.4 SDK
Start PointBase
Populating the Example Database
To populate the database for the Duke's Bookstore examples:
- In a terminal window, go to
<INSTALL>/j2eetutorial14/examples/web/bookstore.- Run
asantcreate-db_common. Thisasanttask runs a PointBase commander tool command to read the filebooks.sqland execute the SQL commands contained in the file. The table namedbooksis created for the userpbpublicin thesun-appserv-samplesPointBase database.- At the end of the processing, you should see the following output:
... [java] SQL> INSERT INTO books VALUES('207', 'Thrilled', 'Ben', [java] 'The Green Project: Programming for Consumer Devices', [java] 30.00, false, 1998, 'What a cool book', 20); [java] 1 row(s) affected [java] SQL> INSERT INTO books VALUES('208', 'Tru', 'Itzal', [java] 'Duke: A Biography of the Java Evangelist', [java] 45.00, true, 2001, 'What a cool book.', 20); [java] 1 row(s) affectedYou can check that the table exists with the PointBase console tool as follows:
- In a terminal window, go to
<J2EE_HOME>/pointbase/tools/serveroption/.- Execute
startconsole.- In the Connect to Database dialog enter
jdbc:pointbase:server://localhost/sun-appserv-samplesin the URL field.- Click OK.
- Expand the SCHEMAS
PBPUBLIC
TABLES nodes. Notice that there is a table named BOOKS.
- To see the contents of the
bookstable:Defining a Data Source in the J2EE Server
Data sources in the J2EE application server implement connection pooling. To define the Duke's Bookstore data source, you first need to create a data pool as follows:
- In a terminal window, go to
<INSTALL>/j2eetutorial14/examples/web/bookstore.- Run
asantcreate-jdbc-connection-pool_common. Thisasanttask runs anasadmincommand to create a JDBC connection pool namedbookstore-pool.- At the end of the processing, you should see the following output:
admin_command_common: [echo] Doing admin task set server.jdbc-connection- pool.bookstore-pool.property.Password=pbpublic [sun-appserv-admin] Executing: set --port 4848 --host localhost --passwordyourpassword--user admin server.jdbc-connection- pool.bookstore-pool.property.Password=pbpublic [sun-appserv-admin] Unable to read system environment. No system environment will be used. [sun-appserv-admin] Attribute property.Password set to pbpublicThen, create the data source as follows:
- In a terminal window, go to
<INSTALL>/j2eetutorial14/examples/web/bookstore.- Run
asantcreate-jdbc-resource_common. Thisasanttask runs anasadmincommand to create a JDBC resource namedjdbc/BookDBthat references thebookstore-poolconnection pool.- At the end of the processing, you should see the following output:
admin_command_common: [echo] Doing admin task create-jdbc-resource -- connectionpoolid bookstore-pool --instance server jdbc/BookDB [sun-appserv-admin] Executing: create-jdbc-resource --port 4848 --host localhost --passwordyourpassword--user admin --connectionpoolid bookstore-pool --instance server jdbc/BookDB [sun-appserv-admin] Created the external JDBC resource with jndiname = jdbc/BookDBWhen you change application server configuration information, the changes are not applied immediately, but are saved into special files, located in
<J2EE_HOME>/domains/domain1/config/backup. Until you apply the changes, they do not take effect. Applying changes is also called reconfiguring the server. When you apply your changes, all changes made to the configuration since the last time you applied changes take effect. Note that restarting the server does not automatically apply the changes.To reconfigure the server:
Configuring the Web Application to Reference a Data Source with JNDI
In order to access a database from a Web application, you must declare resource reference in the application's Web application deployment descriptor (see Declaring References to Environment Entries, Resource Environment Entries, or Resources). The resource reference declares a JNDI name, the type of the data resource, and the kind of authentication used when the resource is accessed. The JNDI name is used to create a data source object in the database helper class
database.BookDB:public BookDB () throws Exception { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB"); con = ds.getConnection(); System.out.println("Created connection to database."); } catch (Exception ex) { System.out.println("Couldn't create connection." + ex.getMessage()); throw new Exception("Couldn't open connection to database: " +ex.getMessage()); }To specify a resource reference to the bookstore data source:
Mapping the Web Application JNDI Name to a Data Source
Since the resource reference declared in the Web application deployment descriptor uses a JNDI name to refer to the data source, you must connect the name to a data source defined by the J2EE application server as follows:
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.